use wd_event::{Context, Event, EventSchedule};

struct Message<V>{
    id : u64,
    value: V,
}
impl<V> Message<V> {
    fn new(id:u64,value:V)->Message<V>{
        Self{id,value}
    }
}
fn generate_event_entity()->Event<EventSchedule>{
    EventSchedule::default()
        .register(|ctx:Context, mut msg:Message<String>|{
            // println!("this is first Message<String> handler ID[{}]--> {}",msg.id,msg.value);
            msg.value.push_str("->one");
            ctx.next(msg)  // no need async
        })
        .register(|ctx:Context, mut msg:Message<String>|async{
            // println!("this is second Message<String>cd  handler ID[{}]--> {}",msg.id,msg.value);
            msg.value.push_str("->two");
            ctx.next(msg).await //all is async
        })
        .register(|ctx:Context, mut msg:Message<i64>|{
            // println!("this is third Message<i64> handle  ID[{}]--> {}",msg.id,msg.value);
            msg.value = 3;
            async move{
                ctx.next(msg).await //part is async
            }
        })
        .build()
}

#[tokio::main]
async fn main() {
    let event = generate_event_entity();

    let stopwatch = std::time::Instant::now();
    let max = 4_00_0000;
    for i in 0..max {
        let res = event.launch(Context::default(),Message::new(1,"hello world".to_string())).await.expect("first test handle failed");
        assert_eq!(res.value.as_str(),"hello world->one->two","Message<String> result assert failed");
        if i % 10_0000 == 0 {
            println!("[{}]->running",i / 100000);
        }
    }
    let t = stopwatch.elapsed();
    println!("run {} times use: {}s",max,t.as_secs());
}
